<?php
require_once("taskView.php");
require_once(CORE_DIR.'taskModel.php');
require_once(CORE_DIR.'taskpointModel.php');
require_once(CORE_DIR.'userModel.php');
require_once(CORE_DIR.'yammerModel.php');
require_once(CORE_DIR.'pointtypeModel.php');

class updateAction extends authAction{

	function __construct(){
		$this->view = new taskView();
	}

	public function run($get, $post, $viewtype) {
		parent::run($get,$post,$viewtype);
		
		$tModel = new taskModel();		
		$tpModel = new taskpointModel();	
		
		if (isset($get["id"]) && isset($get["title"]) && isset($get["important"])){
			$id = $get["id"];
			$projectId = $get["pid"];
			$tskName = htmlspecialchars($get["title"], ENT_QUOTES);
			$tskDesc = htmlspecialchars($get["desc"], ENT_QUOTES);
			$tskImportant = $get["important"];
			$tskType = $get["tasktype"];
			$tskFinishDate = $get["taskfinishdate"];
			$tskStatus = isset($get["status"]) ? $get["status"] : 0;
			
			$ownerId = $get["ownerid"];
			$uids = (isset($get["uids"]) && $get["uids"]!="") ? explode("|", $get["uids"]) : array();
			$uids = array_unique(array_merge((array)$uids, (array)$ownerId));
			
			$taskDto = $tModel->getTaskDto($id);
			$taskOldStatus = $taskDto->getStatus();
			
			$status = 0;
			if ($tModel->updateTask($id, $tskName, $tskDesc, $tskImportant, $tskType, $tskFinishDate, $projectId, $tskStatus)){
				$status = 1;
				$tModel->addUsersToTask($id, $uids, $ownerId);
				
				
			}
			
			if (isset($get["pointtypeid"]) && isset($get["point"])){
				$tpModel->updateTaskPoint($get["pointtypeid"], $id, $get["point"]);
			}
			
			$this->view->setProperty("status", $status);
			$this->view->setProperty("id", $id);
			
////////////////////////////////////////Yammer///////////////////////////////////////////////				
			if($status == 1 && $taskOldStatus == 0)
			{
				$yModel = new yammerModel();
				$uModel = new userModel();
				$ptModel = new pointtypeModel();
				
				$activeUser = $uModel->getActiveUser();
				$taskOwner = $tModel->getOwner($id);
				
				if ( $yModel->isYammerActive($taskOwner->getId()) && $tskStatus != 0 )
				{
					$yammerDto = $yModel->getYammerData($taskOwner->getId());
					
					$task = $tModel->getTask($id);
					$prjName = $tModel->getTasksProjectName($id);
					
					$taskPointDtos = $tpModel->getTaskPointByTaskId($id);
					$taskPointMsg = array();
					foreach ($taskPointDtos as $taskPointDto)
					{
						$pointType = $ptModel->getPointTypesById($taskPointDto->getPointTypeId());
						$taskPointMsg[] = $taskPointDto->getPoint()." ".strtoupper($pointType[0]->getTypeName());
					}
					
					$taskPointMsg = implode(",", $taskPointMsg);
					
					$taskName = urldecode(htmlspecialchars_decode($task['name'], ENT_QUOTES));
					
		            $message = "[".$prjName." +".$taskPointMsg."] ". $taskName."\nTopics: #".$prjName;
		            					
					$result = $yModel->sendMessageToYammer($yammerDto->getOAuthToken(), $yammerDto->getOAuthTokenSecret(), $yammerDto->getOAuthVerifier(), $message);
				}
				
			}
////////////////////////////////////////Yammer///////////////////////////////////////////////				
			
			$this->view->showUpdateTask();
		}
		elseif (isset($get["id"]) && isset($get["reported"]))
		{
			$id = $get["id"];
			$tskReported = $get["reported"];
			
			$status = 0;
			if ($tModel->updateTaskReportFlag($id, $tskReported)){
				$status = 1;
			}	
			$this->view->setProperty("status", $status);
			$this->view->setProperty("id", $id);
			$this->view->showUpdateTask();
		}
		else{
			$this->view->showBadCmd();
		}	

			
	}
}
?>